package com.lzy.webapi.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.dtos.CouponProceedDto;
import com.lzy.product.dtos.ProductInfoCouponDto;
import com.lzy.product.dtos.ProductInfoDto;
import com.lzy.webapi.component.UserToken;
import com.lzy.webapi.facade.ProductCouponFacade;
import com.lzy.webapi.request.ProductCouponRequest;
import com.lzy.webapi.request.ProductRequest;
import com.lzy.webapi.response.ProductInfoCouponResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WIN7 on 2017/12/30.
 */
@Controller
@RequestMapping("api/product/coupon")
public class ProductCouponController extends BaseController{
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductCouponController.class);

    @Autowired
    ProductCouponFacade productCouponFacade;

    /**
     * 卡券产品列表
//     * @param token
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public @ResponseBody Result getProductList(@RequestHeader("token") String token, ProductRequest request) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<ProductInfoDto> pageInfo = productCouponFacade.getProductList(pager);
        if (null != pageInfo) {
            List<ProductInfoCouponDto> productInfoCouponDtoList = new ArrayList<>();
            List<ProductInfoDto> list = pageInfo.getList();
            for (ProductInfoDto piDto : list) {
                if(piDto != null){
                    ProductInfoCouponDto picDto = new ProductInfoCouponDto();
                    picDto.setId(piDto.getId());
                    picDto.setName(piDto.getName());
                    picDto.setLimitTime(piDto.getLimitTime());
                    picDto.setOffice(piDto.getOffice());
                    picDto.setPrice(piDto.getPrice());
                    picDto.setCover(piDto.getCover());
                    picDto.setLabelDiagram(piDto.getLabelDiagram());
                    picDto.setBenefit(piDto.getBenefit());
                    picDto.setProfit(piDto.getProfit());
                    picDto.setProjectList(piDto.getProjectList());
                    picDto.setYieldRatioDtoList(piDto.getYieldRatioDtoList());
                    picDto.setCouponPriceList(piDto.getCouponPriceList());
                    picDto.setCouponList(piDto.getCouponList());
                    picDto.setCouponProceedDto(piDto.getCouponProceedDto());
                    productInfoCouponDtoList.add(picDto);
                }
            }
            return new Result(DozerUtils.maps(productInfoCouponDtoList,ProductInfoCouponResponse.class));
        } else {
            return null;
        }
    }

    /**
     * 产品详情
     * @param
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public @ResponseBody Result getDetail(@RequestHeader("token") String token, ProductCouponRequest request){
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        ProductInfoDto piDto = productCouponFacade.getDetail(request.getId());
        if(piDto != null){
            ProductInfoCouponDto picDto = new ProductInfoCouponDto();
            picDto.setId(piDto.getId());
            picDto.setName(piDto.getName());
            picDto.setLimitTime(piDto.getLimitTime());
            picDto.setOffice(piDto.getOffice());
            picDto.setPrice(piDto.getPrice());
            picDto.setCover(piDto.getCover());
            picDto.setLabelDiagram(piDto.getLabelDiagram());
            picDto.setBenefit(piDto.getBenefit());
            picDto.setProfit(piDto.getProfit());
            picDto.setProjectList(piDto.getProjectList());
            picDto.setYieldRatioDtoList(piDto.getYieldRatioDtoList());
            picDto.setCouponPriceList(piDto.getCouponPriceList());
            picDto.setCouponList(piDto.getCouponList());
            picDto.setCouponProceedDto(piDto.getCouponProceedDto());
            return new Result(picDto);
        }else{
            return null;
        }
    }

    /**
     * 根据利率id获取附加收益
     * @param id
     * @return
     */
    @RequestMapping(value = "/getByYieldRatioId/{id}", method = RequestMethod.GET)
    public @ResponseBody Result getDetail(@RequestHeader("token") String token, @PathVariable("id") Long id){
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        CouponProceedDto couponProceedDto = productCouponFacade.getByYieldRatioId(id);
        if(couponProceedDto != null){
            return new Result(couponProceedDto);
        }
        return new Result();
    }
}
